(function (zn){

    var Bindable = zn.data.Bindable;

    /**
     * @class Dom
     * @namespace zn.ui.dom
     * @desc The element for dom.
     */
    zn.class('zn.ui.dom.DOM', zn.ui.core.Element, {
        partial: true,
        properties: {

        },
        methods: {
            scan: function (model){
                var _model = this.__scanAttributes(model);
                return this.__scanNodes(_model), _model;
            },
            __scanNodes: function (model){
                var _nodes = this._dom.childNodes,
                    _len = _nodes.length,
                    _node = null;

                for(var i= 0; i < _len; i++){
                    _node = _nodes[i];
                    switch(_node.nodeType){
                        case 1:
                            this.__parseElement(_node, model);
                            break;
                        case 3:
                            this.__parseText(_node, model);
                            break;
                    }
                }
            },
            __scanAttributes: function (model){
                var _self = this,
                    _attrs = _self._dom.attributes,
                    _options = null,
                    _name, _key;

                zn.each(_attrs, function (attr){
                    _name = attr.name;
                    if(_name === 'data-view'){
                        return _key = attr.value, -1;
                    }
                    _options = Bindable.parseOptions(attr.value, _self.owner);
                    if(_options){
                        _options.targetPath = _name;
                        _self.bind(_name + '_' + _self.__id__, _options);
                    }
                });



                if(_key){
                    model.defineProperty(_key, {});
                    model.watch(_key, function (value, name){
                        var _oldValue = model[name];
                        console.log(value, name, _oldValue, model);
                        /*
                        value = modelSelf.parseValue(value);
                        modelSelf['_' + name] = value;
                        _self.owner.model = value;*/
                    });

                    var _model = model.get(_key);
                    //_model.set('owner', model);


                    //$view.set(_key, this);
                    if($model !== model){
                        $model.set(_key, _model);
                    }

                    return _model;
                }

                return model;
            },
            __parseText: function (node, model){
                var _content = node.nodeValue.replace(/(^\s*)|(\s*$)/g, ""),
                    _owner = this.owner;
                    _options = Bindable.parseOptions(_content, _owner);

                if(_options){
                    _options.targetPath = 'content';
                    this.children.add(zn.$(node, _owner).bind('content_'+this.__id__, _options));
                }
            },
            __parseElement: function (node, model){
                var _child = zn.$(node, this.owner);
                _child.scan(model);
                this.children.add(_child);
            }
        }
    });

})(zn);
